package com.ysdcrm.crm.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ysdcrm.crm.mapper.IUserRoleRelationMapper;
import com.ysdcrm.crm.mapper.IUsersMapper;
import com.ysdcrm.crm.model.Clocks;
import com.ysdcrm.crm.model.Users;
import com.ysdcrm.crm.model.UsersDTO;

import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.ApiOperation;

@RestController
@ApiOperation("用户管理")
@RequestMapping("user")
@CrossOrigin
public class UsersController {
    
    @Resource
    private IUsersMapper mapper;

    @Resource
    private IUserRoleRelationMapper rMapper;

    @ApiOperation(value = "登录")
    @GetMapping("login")
    public Users login(Users u,HttpSession session){
        Users user = mapper.login(u);
        //将登录用户信息存入session
        session.setAttribute("user", user);
        return user;
    }

    @ApiOperation(value = "修改密码")
    @PutMapping("new")
    public int newPwd(Users u){
        return mapper.newPwd(u);
    }

    @ApiOperation(value = "查询用户")
    @GetMapping
    public List<Users> findAll(UsersDTO dto){
        //条件构造器
        QueryWrapper<Users> qw = new QueryWrapper<>();
        //用户名模糊查询
        if(dto.getName()!=null && dto.getName().length()>0){
            qw.like("name", dto.getName());
        }
        //注册时间区间查询
        if(dto.getRegTime()!=null && dto.getRangeTime().length>0){
            qw.between("reg_time", dto.getRangeTime()[0], dto.getRangeTime()[1]);
        }
        qw.ne("status", 2);//不等于2，表示未删除
        return mapper.selectList(qw);
    }

    @ApiOperation(value = "添加新用户",notes = "用户名不能重复")
    @PutMapping("add")
    public int addUser(Users u){
        //先判断用户名是否存在
        if(mapper.getNameCount(u)>0){
            //已存在
            return 0;
        }else{
            return mapper.insert(u);
        }
    }

    @ApiOperation(value = "禁用/启用")
    @PutMapping("enable")
    public int isUse(int uid){
        return mapper.updateStatus(uid);
    }

    @ApiOperation(value = "修改用户信息",notes = "只能改昵称和头像")
    @PutMapping("edit")
    public int edit(@RequestBody Users u){
        return mapper.edit(u);
    }

    @ApiOperation(value = "删除用户")
    @DeleteMapping
    public int del(int uid){
        return mapper.del(uid);
    }

    @ApiOperation(value = "查询当天签到的咨询师列表")
    @GetMapping("today")
    public List<String> findTodayClockIn(){
        return mapper.findTodayClockIn();
    }

    @ApiOperation(value = "修改权重")
    @PutMapping("weight")
    public int editWeight(Users u){
        return mapper.editWeight(u);
    }

    @ApiOperation(value = "给用户设置角色")
    @PostMapping("set")
    public int addRole(UserRoleRelation r){
        return rMapper.insert(r);
    }

    

}
